--
-- @Author:      
-- @DateTime:    2019-12-20 14:02:03
-- @Description: 活动管理
--  
local skynet = require("skynet")
local Activity = import("manager.Activity")

local table_insert = table.insert
local table_remove = table.remove


local ActivityManager = class("ActivityManager")

-- 初始化
function ActivityManager:ctor()
    
    self.act_list = {} --活动列表
    self.check_time = 300 --检查配置时间
end

function ActivityManager:start()
    self.config = global.config --配置

    skynet.fork( function()
        while true do
            local ok, ret = xpcall(function()
                self:checkActivity()
            end, debug.traceback)
            if not ok then
                errlog(ret)
            end
            skynet.sleep(self.check_time)
        end
    end)
end

-- 重置数据
function ActivityManager:clear()
    for id, v in pairs(self.act_list) do 
        self:removeAct(id)
    end
    self.act_list = {} 
end

-- 获取活动列表
function ActivityManager:getActList()
	return self.act_list
end

--取活动
function ActivityManager:getAct(act_id)
    local act = self.act_list[act_id]
    if not act then
        errlog("not find activity  act_id:",act_id)
        return 
    end
    return act
end

--取活动
function ActivityManager:getActByType(act_type)
    local acts = {}
    for act_id, act in pairs(self.act_list) do 
        if act:getType() == act_type then 
            table_insert(acts, act)
        end
    end
    return acts
end

--添加
function ActivityManager:addAct(act_id, conf_data)	
    --已存在
    local act = self.act_list[act_id]
    if act then    
        act:setConf(conf_data)
        return false
    end  
    local act = Activity.new(act_id, conf_data) 
    -- dbglog("___addAct:", act_id)
    self.act_list[act_id] = act
	return true
end

--移除用户信息
function ActivityManager:removeAct(act_id)
	local act = self.act_list[act_id]
    if not act then
		dbglog("_______removeAct_不存在____",act)
		return false
    end 	
    act:stop()
    self.act_list[act_id] = nil
    dbglog("__removeAct_删除_", act_id )
	return true
end


--设置所有玩家的状态值
function ActivityManager:setAllActValue(name, value)
    for _, act in pairs(self.act_list) do 
        act[name] = value
    end
end

-- 获取当前有用的活动列表
function ActivityManager:getCurActList()
    local act_list = {}
    for act_id, act in pairs(self.act_list) do 
        if act:isEnable() then 
            act_list[act_id] = act
        end
    end
    return act_list
end

--任务型活动
function ActivityManager:getTaskList()
    local act_list = {}
    for act_id, act in pairs(self.act_list) do 
        if act:isEnable() and act:isTask() then 
            act_list[act_id] = act
        end
    end
    return act_list
end

--通知客户端活动内容改变了
function ActivityManager:ntActivityChange()
    local data_list = {}
    local act_list = self:getCurActList()
    local act_data = nil
    for act_id, act in pairs(act_list) do       
        act_data = act:getData()
        if act_data then 
            table_insert(data_list, act_data )      
        end
    end
    dbglog("======ntActivityChange============",data_list)
    local data = {activity_list = data_list}
    R().hallsvr():broadcast('.msg_handler', 'broadcast_msg', 'activity.NTF_ACTIVITY_LIST', data)
end

--通知指定用户活动内容改变了
function ActivityManager:ntUserActivityChange(uid)
    local data_list = {}
    local act_list = self:getCurActList()
    local act_data = nil
    for act_id, act in pairs(act_list) do       
        act_data = act:getData(uid)
        if act_data then 
            table_insert(data_list, act_data )      
        end
    end
    dbglog("======ntUserActivityChange============",data_list)
    local data = {activity_list = data_list}
    R().hallsvr({key=uid}):send('.msg_handler', 'send_msg', uid, 'activity.NTF_ACTIVITY_LIST', data)
end



---------------------------------------------------------------------------



---------------------------------------------------------------------------

--检查活动启动与关闭
function ActivityManager:checkActivity()
    local now = os.time()
    local conf = self.config:getActivity()
    local cur_act_list = {} --配置中的活动id
    for act_id, act_data in pairs(conf) do 
        -- dbglog("_____22222222___act_data___", act_data)
        self:addAct(act_id, act_data)        
        table_insert(cur_act_list, act_id)
    end
    local change = false --
    for act_id, act in pairs(self.act_list) do        
        local begin_time = act:getBeginTime()
        local end_time = act:getEndTime()
        -- dbglog("____act_list____", act_id, act:getOpenState(), os.date("%Y-%m-%d %H:%M:%S", now), os.date("%Y-%m-%d %H:%M:%S", end_time))
        --检查活动开始
        if (now >= begin_time and now < end_time) then 
            if act:start() then 
                change = true
            end
        end
        if (now < begin_time or now > end_time) then 
            if act:stop() then 
                change = true
            end
            self.act_list[act_id] = nil
        end
        --检查活动结束
        if not act:getOpenState() then
            if act:stop() then 
                change = true
            end
            self.act_list[act_id] = nil
        end

        --配置中没有的活动说明已被后台删除了
        local rm = true
        for _, id in pairs(cur_act_list) do 
            if act_id == id then 
                rm = false
                break
            end
        end
        if rm then 
            act:stop()
            self.act_list[act_id] = nil
        end
    end

    if change then --活动有改变通知下客户端
        self:ntActivityChange()
    end
    
end





return ActivityManager

